# -*- coding: utf-8 -*-
# author: AZJ
# date: 2024/11/18
# 假如命运扼住了你的咽喉，你就去挠她的咯吱窝
import time
import subprocess
from datetime import datetime

file = open('log.txt', 'w', encoding='utf-8')
result = subprocess.Popen(['adb', 'logcat'], stdout=subprocess.PIPE,stderr = subprocess.PIPE, bufsize=1)
info_count = 0
waring_count = 0
error_count = 0
debug_count = 0

while True:
    line = result.stdout.readline()
    now = datetime.now()
    if line == "" or result.poll() is not None or line is None:
        break
    if line:
        try:
            info = line.decode('utf-8').strip()
            # print(info)
            file.write(info+'\n')
        except UnicodeDecodeError:
            info = line.decode('gbk', errors='replace').strip()
            # print(info)
            file.write(info+'\n')
        if "beginning of" in info:
            continue
        list_info = info.split(' ')
        # print('info:',list_info[1][6:12])
        execute_time_all = list_info[1][0:12]
        execute_time = float(list_info[1][6:12])
        execute_time_hours = float(list_info[1][0:2])
        execute_time_minutes = float(list_info[1][3:5])
        # print("execute:",execute_time_hours, execute_time_minutes)
        formatted_time = now.strftime('%H:%M:%S.%f')
        now_time_all = formatted_time[0:12]
        now_time = float(formatted_time[6:12])
        now_time_hours = float(formatted_time[0:2])
        now_time_minutes = float(formatted_time[3:5])
        # print("now_time:",now_time_hours, now_time_minutes)
        # print('now:', formatted_time[0:2])
        # print(now_time-execute_time <= 3)
        if now_time-execute_time <= 3 and now_time_hours-execute_time_hours == 0 and now_time_minutes-execute_time_minutes == 0:
            # print("now_time_all:", now_time_all)
            # print("execute_time_all", execute_time_all)
            if list_info[6] == 'W':
                waring_count += 1

            elif list_info[6] == 'E':
                error_count += 1
                print("错误信息: ",line.decode('utf-8').strip())
            elif list_info[6] == 'D':
                debug_count += 1
            elif list_info[6] == 'I':
                info_count += 1
            # print(result.stdout.read())
            print("-------------------------")
            print(f"\t信息:{info_count}")
            print(f"\t警告:{waring_count}")
            print(f"\t错误:{error_count}")
            print(f"\t调试:{debug_count}")
            print("-------------------------")
            # time.sleep(2)
            time.sleep(2)
        # if need_time == '0':
        # print()

